package com.mav.exception;

import com.mav.pojo.Result;
import com.mav.service.iml.DeleteException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

@Slf4j
@RestControllerAdvice
public class GlobalExceptionHandler {
    @ExceptionHandler
    public Result handleException(Exception e){
        log.error("出错啦~",e);
        return Result.error("出错了，请联系管理员");
    }
    @ExceptionHandler
    public Result handleDuplicateKeyException(DuplicateKeyException e){
        log.error("出错啦~",e);
        String message = e.getMessage();
        int index = message.indexOf("Duplicate entry");
        String substring = message.substring(index);
        String[] s = substring.split(" ");
        return Result.error(s[2]+"已存在");
    }
    @ExceptionHandler
    public Result handleDeleteException(DeleteException e){
        log.error(e.getMessage());
        return Result.error(e.getMessage());
    }
}
